package com.appkubes.mall.admin.application.goods.goods.service;

import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.common.utils.data.string.UUIDUtils;
import com.appkubes.mall.admin.application.goods.goods.dao.GoodsIndexDao;
import com.appkubes.mall.core.po.MallGoodsIndex;
import com.appkubes.mall.core.po.MallGoodsIndexExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodsIndexService {

    @Autowired
    GoodsIndexDao goodsIndexDao;

    public int add(MallGoodsIndex model) {
        model.setGoodsIndexKey(UUIDUtils.getUUID());
        model.setCreateTime(DateUtils.DateToTimestamp(new Date()));
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        model.setAppKey(null);
        return goodsIndexDao.insertSelective(model);
    }

    public int update(MallGoodsIndex model) {
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        return goodsIndexDao.updateByPrimaryKeySelective(model);
    }

    public boolean addByGoodsId(String appKey, String goodsKey) {
        if (this.queryByUuidAndGid(appKey,goodsKey) == null) {
            MallGoodsIndex model = new MallGoodsIndex();
            model.setGoodKey(goodsKey);
            this.add(model);
            return true;
        }
        return false;
    }
    public MallGoodsIndex queryByUuidAndGid(String appKey, String goodsKey) {
        MallGoodsIndexExample example = new MallGoodsIndexExample();
        MallGoodsIndexExample.Criteria criteria = example.createCriteria();
        criteria.andAppKeyEqualTo(appKey);
        criteria.andGoodKeyEqualTo(goodsKey);
        List<MallGoodsIndex> list = goodsIndexDao.selectByExample(example);
        if(list.size()>0) return list.get(0);
        return null;
    }
}
